using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using BaseAmd.BaseDevice;
using HslCommunicationEx.OpcUa.SieCnc;
using Catastrophe.Common;
using SiemensAmd.Mmt.Amd;

namespace SiemensAmd.Mmt.DevQt
{
	public class ToolSyncUtil
	{
		public Dictionary<ToolDeviceCacheUnionEntity, DbCompareRetEnum> GetToolCompareRet<T>(List<ToolDeviceCacheUnionEntity> dbList, List<T> readList, ToolInterfaceTypeEnum interfaceType, Func<ToolDeviceCacheUnionEntity, T, ToolDeviceCacheUnionEntity> addHandler, params int[] cacheToolNo) where T : OpcUaToolBaseEntity, new()
		{
			Dictionary<ToolDeviceCacheUnionEntity, DbCompareRetEnum> dictionary = new Dictionary<ToolDeviceCacheUnionEntity, DbCompareRetEnum>();
			Dictionary<string, ToolDeviceCacheUnionEntity> dictionary2 = new Dictionary<string, ToolDeviceCacheUnionEntity>();
			foreach (ToolDeviceCacheUnionEntity db in dbList)
			{
				if (db.IsEmpty)
				{
					continue;
				}
				string text = db.ToolNo.ToString();
				int dNo = db.DNo;
				bool flag = true;
				foreach (T read in readList)
				{
					string text2 = read.ToolNo.ToString();
					int dNo2 = read.DNo;
					string key = "";
					MmtToolExEntity mmtToolExEntity2 = null;
					switch (interfaceType)
					{
					case ToolInterfaceTypeEnum.Mmt:
					{
						mmtToolExEntity2 = read as MmtToolExEntity;
						DefaultInterpolatedStringHandler defaultInterpolatedStringHandler = new DefaultInterpolatedStringHandler(29, 5);
						defaultInterpolatedStringHandler.AppendLiteral("DeviceNo:");
						defaultInterpolatedStringHandler.AppendFormatted(mmtToolExEntity2.DeviceNo);
						defaultInterpolatedStringHandler.AppendLiteral("Mag:");
						defaultInterpolatedStringHandler.AppendFormatted(read.ToolInMag);
						defaultInterpolatedStringHandler.AppendLiteral("PlaceNo:");
						defaultInterpolatedStringHandler.AppendFormatted(read.ToolInPlace);
						defaultInterpolatedStringHandler.AppendLiteral("TNo:");
						defaultInterpolatedStringHandler.AppendFormatted(text2);
						defaultInterpolatedStringHandler.AppendLiteral("DNo:");
						defaultInterpolatedStringHandler.AppendFormatted(dNo2);
						key = defaultInterpolatedStringHandler.ToStringAndClear();
						break;
					}
					case ToolInterfaceTypeEnum.QtDev:
					{
						DefaultInterpolatedStringHandler defaultInterpolatedStringHandler = new DefaultInterpolatedStringHandler(20, 4);
						defaultInterpolatedStringHandler.AppendLiteral("Mag:");
						defaultInterpolatedStringHandler.AppendFormatted(read.ToolInMag);
						defaultInterpolatedStringHandler.AppendLiteral("PlaceNo:");
						defaultInterpolatedStringHandler.AppendFormatted(read.ToolInPlace);
						defaultInterpolatedStringHandler.AppendLiteral("TNo:");
						defaultInterpolatedStringHandler.AppendFormatted(text2);
						defaultInterpolatedStringHandler.AppendLiteral("DNo:");
						defaultInterpolatedStringHandler.AppendFormatted(dNo2);
						key = defaultInterpolatedStringHandler.ToStringAndClear();
						break;
					}
					}
					if (!(text == text2) || dNo != dNo2 || db.ToolInMag != read.ToolInMag || db.ToolInPlace != read.ToolInPlace || (interfaceType == ToolInterfaceTypeEnum.Mmt && (mmtToolExEntity2.DeviceNo != db.DeviceNo || read.ToolInPlace != db.ToolInPlace)))
					{
						continue;
					}
					flag = false;
					if (dictionary2.ContainsKey(key))
					{
						ToolDeviceCacheUnionEntity key2 = dictionary2[key];
						if (!dictionary.ContainsKey(key2))
						{
							dictionary.Add(dictionary2[key], DbCompareRetEnum.Delete);
						}
						flag = true;
					}
					else
					{
						dictionary2.Add(key, db);
					}
					break;
				}
				if (!flag)
				{
					continue;
				}
				switch (interfaceType)
				{
				case ToolInterfaceTypeEnum.QtDev:
					if (!db.FixedLocation || (db.FixedLocation && !db.IsEmpty))
					{
						db.TimeStamp = DateTime.Parse(DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss"));
						dictionary.Add(db, DbCompareRetEnum.Delete);
					}
					break;
				case ToolInterfaceTypeEnum.Mmt:
					db.TimeStamp = DateTime.Parse(DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss"));
					dictionary.Add(db, DbCompareRetEnum.Delete);
					break;
				}
			}
			IEnumerable<IGrouping<int, T>> enumerable = from x in readList
				group x by x.ToolNo;
			foreach (IGrouping<int, T> item in enumerable)
			{
				int key3 = item.Key;
				int edgeIndex = 0;
				IOrderedEnumerable<T> orderedEnumerable = item.OrderBy((T x) => x.EdgePosition);
				foreach (T commEntity in orderedEnumerable)
				{
					int num = edgeIndex;
					edgeIndex = num + 1;
					ToolDeviceCacheUnionEntity toolDeviceCacheUnionEntity = null;
					MmtToolExEntity mmtToolExEntity = null;
					switch (interfaceType)
					{
					case ToolInterfaceTypeEnum.Mmt:
						mmtToolExEntity = commEntity as MmtToolExEntity;
						toolDeviceCacheUnionEntity = dbList.FirstOrDefault((ToolDeviceCacheUnionEntity x) => x.DeviceNo == mmtToolExEntity.DeviceNo && x.ToolInPlace == mmtToolExEntity.ToolInPlace && x.ToolNo == mmtToolExEntity.ToolNo && x.DNo == mmtToolExEntity.DNo);
						break;
					case ToolInterfaceTypeEnum.QtDev:
						toolDeviceCacheUnionEntity = ((!(cacheToolNo?.Contains(commEntity.ToolInMag) ?? false)) ? dbList.FirstOrDefault((ToolDeviceCacheUnionEntity x) => x.ToolInMag == commEntity.ToolInMag && x.ToolInPlace == commEntity.ToolInPlace && x.EdgeIndex == edgeIndex) : dbList.FirstOrDefault((ToolDeviceCacheUnionEntity x) => x.ToolInMag == commEntity.ToolInMag && x.ToolNo == commEntity.ToolNo && x.DNo == commEntity.DNo));
						break;
					}
					DbCompareRetEnum dbCompareRetEnum = ToolSyncUtil.ToolDataCompare(commEntity, toolDeviceCacheUnionEntity);
					if (dbCompareRetEnum == DbCompareRetEnum.None)
					{
						continue;
					}
					if (dbCompareRetEnum == DbCompareRetEnum.Add)
					{
						toolDeviceCacheUnionEntity = addHandler?.Invoke(toolDeviceCacheUnionEntity, commEntity);
						if (cacheToolNo == null || !cacheToolNo.Contains(commEntity.ToolInMag))
						{
							toolDeviceCacheUnionEntity.FixedLocation = true;
							toolDeviceCacheUnionEntity.EdgeIndex = edgeIndex;
						}
					}
					commEntity.TimeStamp = DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss");
					ToolDeviceCacheUnionEntity key4 = ToolSyncUtil.ToolCommToDb(commEntity, toolDeviceCacheUnionEntity);
					dictionary.Add(key4, dbCompareRetEnum);
				}
			}
			return dictionary;
		}

		public static DbCompareRetEnum ToolDataCompare<T>(T src, ToolDeviceCacheEntity dst) where T : OpcUaToolBaseEntity
		{
			if (dst == null)
			{
				return DbCompareRetEnum.Add;
			}
			PropertyInfo[] properties = typeof(T).GetProperties();
			PropertyInfo[] properties2 = typeof(ToolDeviceCacheEntity).GetProperties();
			if (dst.ToolIdent == "1111")
			{
			}
			PropertyInfo[] array = properties;
			foreach (PropertyInfo item in array)
			{
				if (item.Name == "TimeStamp")
				{
					continue;
				}
				object value = item.GetValue(src, null);
				PropertyInfo propertyInfo = properties2.FirstOrDefault((PropertyInfo x) => x.Name == item.Name);
				if (!(propertyInfo == null))
				{
					object value2 = propertyInfo.GetValue(dst, null);
					if (value?.ToString() != value2?.ToString())
					{
						return DbCompareRetEnum.Update;
					}
				}
			}
			return DbCompareRetEnum.None;
		}

		public static ToolDeviceCacheUnionEntity ToolCommToDb<T>(T commEntity, ToolDeviceCacheUnionEntity configEntity) where T : OpcUaToolBaseEntity, new()
		{
			if (commEntity == null)
			{
				throw new ArgumentNullException("commEntity");
			}
			ToolDeviceCacheUnionEntity toolDeviceCacheUnionEntity = DataConverter.DeepCopy<T, ToolDeviceCacheUnionEntity>(commEntity);
			if (configEntity != null)
			{
				toolDeviceCacheUnionEntity.Id = configEntity.Id;
				toolDeviceCacheUnionEntity.FMCNo = configEntity.FMCNo;
				toolDeviceCacheUnionEntity.FixedLocation = configEntity.FixedLocation;
				toolDeviceCacheUnionEntity.EdgeIndex = configEntity.EdgeIndex;
				toolDeviceCacheUnionEntity.MasterUse = configEntity.MasterUse;
				toolDeviceCacheUnionEntity.DeviceNo = configEntity.DeviceNo;
			}
			toolDeviceCacheUnionEntity.TimeStamp = DateTime.Parse(DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss"));
			return toolDeviceCacheUnionEntity;
		}
	}
}
